package com.cz.fra.android.logic;

import com.cz.fra.android.data.FishReport.AuthInfo;
import com.cz.fra.android.entity.Auth;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class AuthLogic {

	private static AuthLogic instance;
	
	private ContentResolver cr;

	private Context mContext;
	
	private AuthLogic(Context context) {
		mContext = context;
		cr = mContext.getContentResolver();
	}
	
	public static AuthLogic getInstance(Context context) {
		if (instance == null) {
			instance = new AuthLogic(context);
		}
		return instance;
	}
	
	public void insertAuthInfo(String username, String password, String isFirstLogin){
		if (cr == null) {
			cr = mContext.getContentResolver();
		}
		String temp = isAuthInDB(username);
		if (temp != null) {
			return;
//			String where = AuthInfo.USERNAME + "=" + temp;
//			cr.delete(AuthInfo.CONTENT_URI, where, null);
		}
		ContentValues cv = new ContentValues();
		cv.put(AuthInfo.USERNAME, username);
		cv.put(AuthInfo.PASSWORD, password);
		cv.put(AuthInfo.isFirstLogin, isFirstLogin);
		cr.insert(AuthInfo.CONTENT_URI, cv);
	}
	
	public Auth getAuthInfo(String username){
		if (cr == null) {
			cr = mContext.getContentResolver();
		}
		Cursor cursor = cr.query(AuthInfo.CONTENT_URI, null, AuthInfo.USERNAME+ "=" + "'" + username+ "'", null, null);
		Auth auth = new Auth();
		if (cursor.moveToFirst()) {
			auth.setUsername(cursor.getString(cursor.getColumnIndex(AuthInfo.USERNAME)));
			auth.setPassword(cursor.getString(cursor.getColumnIndex(AuthInfo.PASSWORD)));
			auth.setIsFirstLogin(cursor.getString(cursor.getColumnIndex(AuthInfo.isFirstLogin)));
			auth.setIsFirstGetReportList(cursor.getString(cursor.getColumnIndex(AuthInfo.isFirstGetReportList)));
		}
		cursor.close();
		return auth;
	}
	
	public Auth getAuthInfo(){
		if (cr == null) {
			cr = mContext.getContentResolver();
		}
		Cursor cursor = cr.query(AuthInfo.CONTENT_URI, null, null, null, null);
		Auth auth = new Auth();
		if (cursor.moveToFirst()) {
			auth.setUsername(cursor.getString(cursor.getColumnIndex(AuthInfo.USERNAME)));
			auth.setPassword(cursor.getString(cursor.getColumnIndex(AuthInfo.PASSWORD)));
			auth.setIsFirstLogin(cursor.getString(cursor.getColumnIndex(AuthInfo.isFirstLogin)));
			auth.setIsFirstGetReportList(cursor.getString(cursor.getColumnIndex(AuthInfo.isFirstGetReportList)));
		}
		cursor.close();
		return auth;
	}
	
	public String isAuthInDB(String username){
		String user = null;
		Cursor cursor = cr.query(AuthInfo.CONTENT_URI, null, AuthInfo.USERNAME+ "=" + "'" + username + "'", null, null);
		if (cursor.moveToFirst()) {
			user = cursor.getString(cursor.getColumnIndex(AuthInfo.USERNAME));
		}
		if (cursor != null) {
			cursor.close();
		}
		return user;
	}
	
	/**
	 * Update IsFirstGetReportList field 
	 * @param auth
	 */
	public void updateAuthInfo(Auth auth){
		if (cr == null) {
			cr = mContext.getContentResolver();
		}
		String temp = isAuthInDB(auth.getUsername());
		if( temp!= null){
			String where = AuthInfo.USERNAME + "=" + "'" + auth.getUsername()+ "'";
			cr.delete(AuthInfo.CONTENT_URI, where, null);
		}
		cr.insert(AuthInfo.CONTENT_URI, auth.instanceToCV());
	}
}
